﻿using System;
using System.Collections.Generic;
using System.Web.UI;

using Swaf;
using Swaf.BizObj;
using Swaf.Container;

using Syrinx.Gui.AspNet;
namespace Calendars.Gui.AspNet
{
	[Serializable]
	public class CalEventsMenuItem : MenuItem
	{
		protected int m_maxElements = -1;
		protected string m_calId;
		protected string m_itemTemplate = null;
		protected List<Articles.Gui.AspNet.FilterOption> m_filters = new List<Articles.Gui.AspNet.FilterOption>();
		protected List<Articles.Gui.AspNet.SortOption> m_orderBy = new List<Articles.Gui.AspNet.SortOption>();

		public string CalendarId { get { return m_calId; } set { m_calId = value; } }
		public int MaxElements { get { return m_maxElements; } set { m_maxElements = value; } }

		[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
		public string Template { get { return m_itemTemplate; } set { m_itemTemplate = value; } }

		[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
		public List<Articles.Gui.AspNet.FilterOption> Filter { get { return m_filters; } }

		[PersistenceModeAttribute(PersistenceMode.InnerProperty)]
		public List<Articles.Gui.AspNet.SortOption> Sort { get { return m_orderBy; } }

		public override void setupMenuItem(Menu parent)
		{
			m_items = new List<MenuItem>();
			NameAddressSupportDictionaryWrapper articleWrapper = new NameAddressSupportDictionaryWrapper(null);
			IBizObjList events = getEvents();
			foreach (IBizObj ev in events)
			{
				MenuItem mi = new MenuItem((string)ev.get("title"), string.Format("/articles/{0}.aspx", ev.get("uniqueId")));
				if (!Cmn.IsEmpty(Template))
				{
					articleWrapper.InnerElement = ev;
					mi.Text = Swaf.Application.currentApp.resMgr.resolve(Template, articleWrapper);
				}
				Items.Add(mi);
			}
		}

		protected IBizObjList getEvents()
		{
			IResults rc = Swaf.Application.currentApp.ruleMgr.execute("getCalendarEvents",
				CalendarId, Articles.Gui.AspNet.FilterSortOption.createFilterSortOption(Filter, Sort),
				DetailLevel.basicDetails, false, MaxElements);
			IBizObjList events = rc.CallResults as IBizObjList;
			return events;
		}

	}
}
